package org.zachary.imitation.raft.core;

import org.zachary.imitation.raft.NodeId;
import org.zachary.imitation.raft.PeerId;
import org.zachary.imitation.raft.option.ReplicatorGroupOptions;
import org.zachary.imitation.raft.util.Describer;
import org.zachary.imitation.raft.util.ThreadId;

/**
 * 复制器组的接口
 */
public interface ReplicatorGroup extends Describer {

	/** 初始化复制器组 */
	boolean init(final NodeId nodeId, final ReplicatorGroupOptions opts);

	/** 向复制器组（{@link ReplicatorGroup}）中添加一个复制器（{@link Replicator}）*/
	default boolean addReplicator(final PeerId peer) {
		return addReplicator(peer, ReplicatorType.Follower);
	}

	/** 向复制器组（{@link ReplicatorGroup}）中添加一个复制器（{@link Replicator}）*/
	default boolean addReplicator(final PeerId peer, ReplicatorType replicatorType) {
		return addReplicator(peer, replicatorType, true);
	}

	/** 向复制器组（{@link ReplicatorGroup}）中添加一个复制器（{@link Replicator}）
	 *
	 * @param peer 代表着 Follower
	 * @param replicatorType 复制器的类型，相对于 Leader 而言，其它的节点可能是 Follower，也可能是一个 Leaner
	 * @param sync TODO 立即同步？
	 */
	boolean addReplicator(final PeerId peer, ReplicatorType replicatorType, boolean sync);

	/** 获取指定 Follower 对应的 Replicator 实例 */
	ThreadId getReplicator(final PeerId peer);

	/** 检查 Replicator 实例 */
	void checkReplicator(final PeerId peer, final boolean lockNode);

	/** 检查 Replicator 实例 */
	boolean resetTerm(final long newTerm);

	/** 复制器组中是否包含指定 Follower 的复制器 */
	boolean contains(final PeerId peer);
}
